package com.book.service;

import com.book.pojo.UserInfo;
import com.book.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description:TODO
 * @date 2023/10/23 21:57
 */
@Service
public class UserBridgeService {

    @Autowired
    private UserRepository repository;


    public String login(String account, String password) {
        UserInfo userInfo = repository.findByUserNameAndUserPassword(account, password);
        if(null == userInfo) {
            return "Login failed";
        }
        return "Login successfully";
    }

    public String register(UserInfo info) {
        if (checkUserExists(info.getUserName())) {
            throw new RuntimeException("User already registered");
        }
        info.setCreateDate(new Date());
        info.setEmail("12341515@163.com");
        repository.save(info);
        return "Register successfully";

    }

    public boolean checkUserExists(String userName) {
        UserInfo user = repository.findByUserName(userName);
        if(null == user) {
            return false;
        }
        return true;

    }

    public String login3Rd(HttpServletRequest request) {

        return null;
    }
}
